<template>
  <common-select
    :options="appOptions"
    :defaultValue="defaultValue"
    @changeoption="changeOption"
    :allow-clear="clear"
    :show-search="true"
    :disabled="disabled"
    :disabledOptions="disabledOptions"
    :mode="mode"
    :max-tag-count="maxTagCount"
    :placeholder="$t('pleaseSelect') + $t('application').toLowerCase()"
  ></common-select>
</template>
<script>
import CommonSelect from '@/components/SelfComponents/CommonSelect.vue'
import sotaVersionApi from '@/api/modules/software/sotaVersion'
export default {
  props: {
    //
    query: {
      type: Object,
      default () {
        return {
        }
      }
    },
    // 是否需要全部选项
    hasAll: {
      type: Boolean,
      default: true
    },
    clear: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    defaultValue: {
      type: [String, Array, undefined, Object],
      default () {
        return undefined
      }
    },
    // 禁用下拉配置
    disabledOptions: {
      type: Array,
      default () {
        return []
      }
    },
    maxTagCount: {
      type: Number,
      default: 6
    },
    mode: {
      type: String,
      default: 'default'
    }
  },
  components: {
    CommonSelect
  },
  computed: {},
  data () {
    return {
      appOptions: []
    }
  },
  watch: {
    query: {
      handler () {
        this.getData()
      },
      deep: true,
      immediate: true
    }
  },
  created () {

  },
  methods: {
    getData () {
      sotaVersionApi.softwareList().then((res) => {
        const arr = this.hasAll ? [
          {
            id: '',
            name: this.$t('all') + this.$t('application')
          }
        ] : []
        this.appOptions = arr.concat(res.data.map(item => {
          return {
            id: item.packageName,
            name: item.name
          }
        }))
      })
    },
    changeOption (val) {
      this.$emit('changeoption', val)
    }
  }
}
</script>
